<%--
  Created by IntelliJ IDEA.
  User: WYX
  Date: 2024/3/29
  Time: 8:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.math.BigInteger" %>
<html>
<head>
    <title>day01.2.jsp</title>
</head>
<body>
<%!
    public static String reverse(String s){
        StringBuffer buffer=new StringBuffer(s);
        StringBuffer reverseBuffer=buffer.reverse();
        return reverseBuffer.toString();
    }
%>
<%
    String regex = "[1-9][0-9]*";
    String startNumber = request.getParameter("num");
    if (startNumber == null || startNumber.isEmpty())
        startNumber = "520";
    if(!startNumber.matches(regex)){
        response.sendRedirect("day01.1.jsp");
        return;
    }
    long step=1;

    BigInteger num=new BigInteger(startNumber);
    BigInteger reverseNum=new BigInteger(reverse(num.toString()));
    BigInteger addData=num.add(reverseNum);
    out.println("<br>"+num+"+"+reverseNum+"="+addData);
    BigInteger p=new BigInteger(reverse(addData.toString()));
    while(!addData.equals(p)){
        num=new BigInteger(addData.toString());
        reverseNum=new BigInteger(reverse(num.toString()));
        addData=num.add(reverseNum);
        p=new BigInteger(reverse(addData.toString()));
        out.print("<br>"+num+"+"+reverseNum+"="+addData);
        step++;
    }
    out.print("<br>"+step+"步得到回文："+addData);
%>
</body>
</html>
